package cn.tedu.blog.server.controller.system;


import cn.tedu.blog.server.pojo.dto.MessagePushNewAdd;
import cn.tedu.blog.server.pojo.vo.MessagePushListVO;
import cn.tedu.blog.server.service.IMessagePushService;
import cn.tedu.blog.server.utils.JsonPage;
import cn.tedu.blog.server.utils.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

@Api(tags = "推送信息管理")
@Slf4j
@RestController
@RequestMapping("/system/message/push")
public class MessagePushController {


    @Autowired
    private IMessagePushService messagePushService;


    @ApiOperation("新增推送信息")
    @ApiOperationSupport(order = 100)
    @PostMapping("/creat/message")
    public JsonResult createMessage(@RequestBody MessagePushNewAdd messagePushNewAdd){
        messagePushService.createMessage(messagePushNewAdd);
        return JsonResult.ok();
    }


    @ApiOperation("修改推送信息状态")
    @ApiOperationSupport(order = 200)
    @PostMapping("/update/messageStatus")
    public JsonResult updateMessageStatus(@RequestBody MessagePushNewAdd messagePushNewAdd){
        messagePushService.updateStatusById(messagePushNewAdd);
        return JsonResult.ok();
    }



    @ApiOperation("根据用户ID修改推送信息状态")
    @ApiOperationSupport(order = 200)
    @GetMapping("/update/messageStatus")
    public JsonResult updateStatusByUserId(Principal principal){
        String temp = principal.getName().split("=")[1];
        long userId = Long.valueOf(temp.substring(0,temp.length()-1).split(",")[0]);
        log.debug("当前登录用户ID：{}，准备清空推送信息",userId);
        messagePushService.updateStatusByUserId(userId);
        return JsonResult.ok();
    }



    @ApiOperation("一键已读")
    @ApiOperationSupport(order = 300)
    @GetMapping("/updateReadStatusAll")
    public JsonResult updateReadStatusAll(Principal principal){
        String temp = principal.getName().split("=")[1];
        long userId = Long.valueOf(temp.substring(0,temp.length()-1).split(",")[0]);
        log.debug("当前登录用户ID：{}，一键已读",userId);
        messagePushService.updateReadStatusAll(userId);
        return JsonResult.ok();
    }


    @ApiOperation("查询未读消息书数量")
    @ApiOperationSupport(order = 400)
    @GetMapping("/ReadStatusCount")
    public int ReadStatusCount(Principal principal){
        String temp = principal.getName().split("=")[1];
        long userId = Long.valueOf(temp.substring(0,temp.length()-1).split(",")[0]);
        log.debug("当前登录用户ID：{}",userId);
        return messagePushService.ReadStatusCount(userId);
    }


    @ApiOperation("查询启用状态下所有推送消息的数量")
    @ApiOperationSupport(order = 401)
    @GetMapping("/selectMessageByUserIdCountAll")
    public int selectMessageByUserIdCountAll(Principal principal){
        String temp = principal.getName().split("=")[1];
        long userId = Long.valueOf(temp.substring(0,temp.length()-1).split(",")[0]);
        log.debug("当前登录用户ID：{}",userId);
        return messagePushService.selectMessageByUserIdCountAll(userId);
    }


    @ApiOperation("查询启用状态下所有推送消息")
    @ApiOperationSupport(order = 402)
    @PostMapping("/selectMessageByUserId")
    public JsonPage<MessagePushListVO> selectMessageByUserId(@RequestBody MessagePushListVO messagePushListVO,Principal principal){
        String temp = principal.getName().split("=")[1];
        long userId = Long.valueOf(temp.substring(0,temp.length()-1).split(",")[0]);
        log.debug("当前登录用户ID：{}",userId);
        messagePushListVO.setUserId(userId);
        return messagePushService.selectMessageByUserId(messagePushListVO);
    }
}
